package cn.com.nes.site.service.system.message.impl;

import cn.com.nes.common.em.WorkOrderNotificationEnum;
import cn.com.nes.mybatis.agent.ope.entity.IsUserPhoneInfo;
import cn.com.nes.mybatis.agent.ope.mapper.IsInstallWorkOrderInfoMapper;
import cn.com.nes.mybatis.agent.ope.mapper.IsUserPhoneInfoMapper;
import cn.com.nes.mybatis.agent.system.mapper.TUserMapper;
import cn.com.nes.mybatis.callcenter.mapper.CallWorkOrderInfoMapper;
import cn.com.nes.site.entity.UserInfo;
import cn.com.nes.site.module.service.monitor.EquipmentInfoService;
import cn.com.nes.site.service.system.message.MessageInfoService;
import cn.com.nes.site.service.system.message.MessagePush;
import cn.com.nes.site.service.system.region.XzqhService;
import cn.com.nes.site.service.workOrder.dto.BadgeCount;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: ShenWei
 * @Date: 2024/03/11/17:22
 */
@Service
public class MessageInfoServiceImpl implements MessageInfoService {
    private static final String USER_TYPE_CODE_MAINTENANCE_WORKER = "08";  // 用户类型 - 安装维修

    @Resource
    IsUserPhoneInfoMapper isUserPhoneInfoMapper;

    @Autowired
    MessagePush messagePush;

    @Autowired
    XzqhService xzqhService;

    @Autowired
    EquipmentInfoService equipmentInfoService;

    @Resource
    TUserMapper tUserMapper;

    @Resource
    CallWorkOrderInfoMapper callWorkOrderInfoMapper;

    @Resource
    IsInstallWorkOrderInfoMapper installWorkOrderInfoMapper;

    /**
     * 向负责人推送指派工单的通知
     *
     * @param userId      负责人id
     * @param workOrderId 工单id
     */
    @Override
    public void pushAssignNotification(String userId, String workOrderId) {
        //推送app消息
        List<IsUserPhoneInfo> userPhoneInfos = isUserPhoneInfoMapper.selectByUserId(userId);
        if (CollUtil.isNotEmpty(userPhoneInfos)) {
            messagePush.pushWorkOrderNotification(userPhoneInfos, workOrderId, WorkOrderNotificationEnum._assign);
        }
    }

    /**
     * 向维修人员推送新工单通知
     *
     * @param userId      维修人员id
     * @param workOrderId 工单id
     */
    @Override
    public void pushNewWorkOrderNotification(String userId, String workOrderId) {
        //推送app消息
        List<IsUserPhoneInfo> userPhoneInfos = isUserPhoneInfoMapper.selectByUserId(userId);
        if (CollUtil.isNotEmpty(userPhoneInfos)) {
            messagePush.pushWorkOrderNotification(userPhoneInfos, workOrderId, WorkOrderNotificationEnum._new);
        }
    }

    @Override
    public BadgeCount getBadgeCount(String deviceId) {
        if (StrUtil.isNotBlank(deviceId) && deviceId.contains(",")) {
            deviceId = deviceId.split(",")[0];
        }
        UserInfo userInfo = isUserPhoneInfoMapper.getUserInfo(deviceId);
        if (ObjectUtil.isNull(userInfo)) {
            return new BadgeCount(0, 0, 0, 0, 0, 0, 0);
        }
        return getBadgeCount(userInfo.getNes_companyId(), userInfo.getId(), userInfo.getDepartmentId(), Byte.valueOf(userInfo.getDataRange()));
    }

    @Override
    public BadgeCount getBadgeCount(String manufactureId, String userId, String departmentId, Byte dataRange) {
        // 查询用户权限
        String areaCode = xzqhService.getUserAreaCode(userId);
        String installTableAlias = "v";
        String userTableAlias = "iui";
        // 组装一个sql片段
        String cxtj = equipmentInfoService.getCxtjByDataRange(null, areaCode, userId, departmentId, manufactureId, installTableAlias, userTableAlias);
        String userType = tUserMapper.getUserTypeByUserId(userId);
        boolean maintenanceWorker = USER_TYPE_CODE_MAINTENANCE_WORKER.equals(userType);
        // 查询待接单工单数量
        int newWorkOrder = callWorkOrderInfoMapper.getWorkOrderCount(manufactureId, userId, CollUtil.newArrayList(1), -1, cxtj);
        // 查询待审核的工单数量, 如果是维修工,则只查询自己待审核的维修工单数量
        int unauditedWordOrderCount = callWorkOrderInfoMapper.getWorkOrderCount(manufactureId, maintenanceWorker ? userId : "", null, 0, cxtj);
        // 查询待指派的工单数量
        int pendingDispatchCount = callWorkOrderInfoMapper.getPendingDispatchCount(manufactureId, userId);
        // 查询待审核的设备数量, 如果是维修工, 则只查询自己待审核的安装工单数量
        int unauditedInstallCount = installWorkOrderInfoMapper.getEquipmentBadgeCount(manufactureId, maintenanceWorker ? userId : "", 0, cxtj, 1);
        // 查询待审核的登记设备数量, 如果是维修工, 则只查询自己待审核的安装工单数量
        int unauditedRegisteredEquipmentCount = installWorkOrderInfoMapper.getEquipmentBadgeCount(manufactureId, maintenanceWorker ? userId : "", 0, cxtj, 2);
        //维修人员额外提醒审核失败的维修工单数和安装工单数
        if (maintenanceWorker) {
            int failedWorkOrderCount = callWorkOrderInfoMapper.getWorkOrderCount(manufactureId, userId, null, 2, cxtj);
            int failedInstallCount = installWorkOrderInfoMapper.getEquipmentBadgeCount(manufactureId, userId, 2, cxtj, 1);
            int failedRegisteredCount = installWorkOrderInfoMapper.getEquipmentBadgeCount(manufactureId, userId, 2, cxtj, 2);
            unauditedWordOrderCount += failedWorkOrderCount;
            unauditedInstallCount += failedInstallCount;
            unauditedRegisteredEquipmentCount += failedRegisteredCount;
        }
        final int totalCount = newWorkOrder + unauditedWordOrderCount + pendingDispatchCount + unauditedInstallCount;
        return new BadgeCount(totalCount, newWorkOrder, unauditedWordOrderCount, pendingDispatchCount,
                unauditedInstallCount, unauditedRegisteredEquipmentCount, 0);
    }
}
